#include "_usart.h"
#include "string.h"
#include "stdio.h"
#include "jy61p/jy61p.h"

uint8_t rx_buffer[RX_BUFFER_SIZE];

void usart_receive_data(UART_Regs *uartx, uint8_t *data, uint8_t size)
{
    // HAL_UART_Receive_IT(uartx, rx_buffer, size);
    memcpy(data, rx_buffer, size);
}

void usart_send_data(UART_Regs *uartx, uint8_t *data, uint8_t size)
{
    for (int i = 0; i < size; i++) 
    {
        DL_UART_transmitDataBlocking(uartx, data[i]);
    }
}

void UART_0_INST_IRQHandler()
{
    static uint8_t is_ready = 0;
    static uint8_t ex_data = 0;
    static uint8_t next_data_size = 0;
    
    rx_buffer[0] = DL_UART_receiveData(UART_0_INST);
    if (next_data_size)
    {
        jy61p_buffer[next_data_size - 1] = rx_buffer[0];
        next_data_size--;

        if (next_data_size == 0)
        {
            jy61p_usart_RxCallback();
        }
        
    }
    

    if (ex_data == 0x55 && rx_buffer[0] == 0x53)
    {
        next_data_size = 6;
    }
    
    ex_data = rx_buffer[0];

}

